package com.liushuai.phoneguardian;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.liushuai.phoneguardian.ui.SettingView;
import com.liushuai.phoneguardian.utils.ActivityUtils;

public class LostActivity extends Activity {

	private SharedPreferences sp;
	private ImageView iv_lostfind_status;
	private TextView tv_lostfind_number;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lost);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		tv_lostfind_number = (TextView) findViewById(R.id.tv_lostfind_number);
		tv_lostfind_number.setText(sp.getString("safenumber", ""));
		iv_lostfind_status = (ImageView) findViewById(R.id.iv_lostfind_status);
		boolean protecting = sp.getBoolean("protecting", false);
		if(protecting){
			iv_lostfind_status.setImageResource(R.drawable.lock);
		}else{
			iv_lostfind_status.setImageResource(R.drawable.unlock);
		}
		if(!isSetup()){
			Intent intent = new Intent(this, Setup1Activity.class);
			startActivity(intent);
			finish();
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_lost_activity, menu);
		return super.onCreateOptionsMenu(menu);
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.item1_menu_lost_find:

			AlertDialog.Builder builder = new Builder(this);
			builder.setTitle("设置防盗名称");
			final EditText text = new EditText(this);
			text.setHint("请输入防盗名称");
			builder.setView(text);
			builder.setPositiveButton("Sure", new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					String newname = text.getText().toString();
					Editor et = sp.edit();
					et.putString("newname", newname);
					et.commit();
					dialog.dismiss();

				}
			});

			builder.show();
			break;

		}
		return super.onOptionsItemSelected(item);
	}
	//end of onOptionsItemSelected()

	public boolean isSetup() {
		return sp.getBoolean("setup", false);
	}
	public void reEntrySetup(View view){
		ActivityUtils.startActivityAndFinish(this, Setup1Activity.class);
	}
}
